#pragma once
#include<iostream>
#include<pthread.h>
#include<signal.h>
#include "tcpserver.hpp"
#include"protocol.hpp"
#include"task.hpp"
#include"threadpool.hpp"

#define PORT 8081
class HttpServer{
    private:
        int port; 
        TcpServer* st;
        int listen_sock;
    public:
        HttpServer(int _port=PORT):port(_port),st(nullptr),listen_sock(-1)
        {}
        void initiailzer(){
            signal(SIGPIPE,SIG_IGN);
            st=TcpServer::GetServerObj(port);
            listen_sock=st->RenturnSock();
        }
        void Loop(){
            for(;;){
                struct sockaddr_in oth;
                memset(&oth,0,sizeof(oth));
                socklen_t len=sizeof(oth);
                int sock=accept(listen_sock,(struct sockaddr*)&oth,&len);
                if(sock<0)
                    continue;
                Task t(sock);
                ThreadPool* p=ThreadPool::getthreadpool();
                p->Push(t);
            }
        }
        ~HttpServer(){
            if(listen_sock>=0)
                close(listen_sock);
        }
};
